/**
 * Created on 2018/8/11.
 */
package jetcache.samples.springboot;

import com.alibaba.fastjson2.JSON;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.CacheManager;
import com.alicp.jetcache.anno.support.ConfigProvider;
import com.alicp.jetcache.template.QuickConfig;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * @author huangli
 */
@Component
public class MyServiceImpl implements MyService, InitializingBean {

    @Autowired
    private UserService userService;

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private ConfigProvider configProvider;

    private Cache<String, String> orderCache;

    @Override
    public void afterPropertiesSet() {
        QuickConfig quickConfig = QuickConfig.newBuilder("orderCache")
                .expire(Duration.ofSeconds(100)).build();
        orderCache = cacheManager.getOrCreateCache(quickConfig);
    }

    @Override
    public void createCacheDemo() {
        orderCache.put("K1","V1");
        System.out.println("get from orderCache:" + orderCache.get("K1"));
    }

    @Override
    public void cachedDemo() {
//        userService.loadUser(1);
        User user = userService.loadUser(1L);
        System.out.println("userService.loadUser(1) = " + JSON.toJSONString(user));
    }

    @Override
    public void updateUser(User user) {
        userService.updateUser(user);
    }


}
